﻿// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.Text;
using Settings.UI.Library.Enumerations;

namespace SvgPreviewHandler
{
    internal sealed class SvgHTMLPreviewGenerator
    {
        private const string CheckeredBackgroundShadeLight = """
            url('');
            """;

        private const string CheckeredBackgroundShadeMedium = """
            url('');
            """;

        private const string CheckeredBackgroundShadeDark = """
            url('');
            """;

        private const string HtmlTemplateSolidColor = """
            <html>
                <body style="background-color: {0}">
                    {1}
                </body>
            </html>
            """;

        private const string HtmlTemplateCheckered = """
            <html>
                <body style="background-image: {0}">
                    {1}
                </body>
            </html>
            """;

        private readonly Settings settings = new();

        private static readonly CompositeFormat HtmlTemplateSolidColorCompositeFormat = System.Text.CompositeFormat.Parse(HtmlTemplateSolidColor);
        private static readonly CompositeFormat HtmlTemplateCheckeredCompositeFormat = System.Text.CompositeFormat.Parse(HtmlTemplateCheckered);

        public string GeneratePreview(string svgData)
        {
            var colorMode = (SvgPreviewColorMode)settings.ColorMode;
            return colorMode switch
            {
                SvgPreviewColorMode.SolidColor => string.Format(CultureInfo.InvariantCulture, HtmlTemplateSolidColorCompositeFormat, ColorTranslator.ToHtml(settings.SolidColor), svgData),
                SvgPreviewColorMode.Checkered => string.Format(CultureInfo.InvariantCulture, HtmlTemplateCheckeredCompositeFormat, GetConfiguredCheckeredShadeImage(), svgData),
                SvgPreviewColorMode.Default or _ => string.Format(CultureInfo.InvariantCulture, HtmlTemplateSolidColorCompositeFormat, ColorTranslator.ToHtml(settings.ThemeColor), svgData),
            };
        }

        private string GetConfiguredCheckeredShadeImage()
        {
            var checkeredShade = (SvgPreviewCheckeredShade)settings.CheckeredShade;
            return checkeredShade switch
            {
                SvgPreviewCheckeredShade.Light=> CheckeredBackgroundShadeLight,
                SvgPreviewCheckeredShade.Medium => CheckeredBackgroundShadeMedium,
                SvgPreviewCheckeredShade.Dark or _ => CheckeredBackgroundShadeDark,
            };
        }
    }
}
